package com.song.mybatis_test;

import com.song.dao.BlogMapper;
import com.song.dao.SequenceDao;
import com.sun.tools.doclint.Env;
import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @Description: test：类描述
 * @author: songzhangyong
 * @Date: 2019/11/9 10:56
 */

public class MyBatisExecutorTest {

    static SqlSessionFactory factory = null;
    static SqlSession session = null;

    static {
        String resource = "/Users/song/Documents/D/songFile/源码解读/mybatis/mybatissong/src/main/resources/" +
                "mybatis.xml";
//        InputStream inputStream = Resources.getResourceAsStream(resource);
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File(resource));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        //这一步完成了mybatis.xml 文件的所有解析操作
        factory = new SqlSessionFactoryBuilder().build(inputStream);
        factory.getConfiguration().addMapper(SequenceDao.class);
        factory.getConfiguration().addMapper(BlogMapper.class);

        //默认的非自动提交，所以下面的插入需要走一个commit操作
        session = factory.openSession();
    }

    @Test
    public void insert() {
//        SequenceDao mapper = session.getMapper(SequenceDao.class);
//        mapper.insert("song1", 1);
//        mapper.insert("song2", 2);
//        mapper.insert("song3", 3);
//        mapper.insert("song4", 4);
//        mapper.insert("song5", 5);
//        mapper.insert("song6", 6);
//        mapper.insert("song7", 7);
//        mapper.insert("song8", 8);
//        session.commit();
        System.out.println("fdfdfdfdfd");
    }


    @Test
    public void executor_simple() {
        session = factory.openSession(ExecutorType.SIMPLE, true);
        SequenceDao mapper = session.getMapper(SequenceDao.class);
        System.out.println(mapper.queryById(2));
        System.out.println(mapper.queryById(3));
    }


    @Test
    public void executor_reuse() {
        /**
         * song-s 这里的sql只编译了一次 。 也就是 JDBC statement . 只要sql相同
         *  ==>  Preparing: select * from mybatis_test where id = ?
         *  ==> Parameters: 2(Integer)
         *  <==      Total: 1
         * [UserInfo{id=2, name='song2', age='2'}]
         *
         * ==> Parameters: 3(Integer)
         * <==      Total: 1
         * [UserInfo{id=3, name='song3', age='3'}]
         *
         */
        session = factory.openSession(ExecutorType.REUSE, true);
        SequenceDao mapper = session.getMapper(SequenceDao.class);
        System.out.println(mapper.queryById(2));
        System.out.println(mapper.queryById2(4));
        System.out.println(mapper.queryById(3));
    }


    @Test
    public void executor_batch() {

        session = factory.openSession(ExecutorType.BATCH, true);
        SequenceDao mapper = session.getMapper(SequenceDao.class);
        mapper.insert("song10", 10);
        mapper.updateName("song2_2", 2);
        mapper.insert("song11", 11);
        mapper.insert("song12", 12);

        // song-s 刷新批处理语句,返回批处理结果。 如果不进行调用，这里就不会入库
        List<BatchResult> batchResults = session.flushStatements();
        //batchResults 的size是3
        //1、sql相同 2、mapperStatement 也就是同一个dao对象 3、执行的方法是连续的 也就是 statement相同
        System.out.println(batchResults.size());


    }

}
